<?php

use Illuminate\Database\Seeder;
use Yangyifan\Library\UtilityLibrary;

class RolesTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        echo "Start: " . __CLASS__ . " \r\n";

        $menus = [
            ['name' => '超级管理员', 'children' => [
                ['name' => '管理员'],
                ['name' => '运维'],
            ]],
            ['name' => '运营总监', 'children' => [
                ['name' => '运营经理'],
                ['name' => '运营主管'],
                ['name' => '运营专员'],
            ]],
            ['name' => '市场总监', 'children' => [
                ['name' => '市场经理'],
                ['name' => '市场主管'],
                ['name' => '市场专员'],
            ]],
            ['name' => '客服经理', 'children' => [
                ['name' => '客服主管'],
                ['name' => '客服专员'],
            ]],
        ];

        // 循环的去遍历添加
        foreach ($menus as $menu) {
            // 创建一个账户
            $parentId = $this->add($menu);

            // 如果有子级，则继续新增子级
            if (UtilityLibrary::isArray($menu['children'])) {
                foreach ($menu['children'] as $childrenMenu) {
                    $this->add(array_merge($childrenMenu, ['parent_id' => $parentId]));
                }
            }
        }

        echo "Finished: " . __CLASS__ . " \r\n";
    }

    /**
     * 添加数据
     *
     * @param $menu
     *
     * @return int
     */
    protected function add($menu)
    {
        $model = \Yangyifan\Administrator\Access\Model\Roles\Roles::create([
            'name'      => $menu['name'],
            'status'    => 1,
            'parent_id' => $menu['parent_id'] ?? 0,
        ]);

        return $model->id;
    }
}
